package org.dromara.xyyw.controller;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xyyw.domain.bo.XyywProductBo;
import org.dromara.xyyw.domain.query.XyywProductQuery;
import org.dromara.xyyw.domain.vo.XyywProductVo;
import org.dromara.xyyw.service.IXyywProductService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 产品
 *
 * @author yf
 * @date 2024-05-29
 */
@Validated
@RestController
@RequestMapping("/xyyw/product")
public class XyywProductController extends BaseController {

    @Autowired
    private IXyywProductService xyywProductService;

    /**
     * 查询产品列表
     */
    @SaCheckPermission("xyyw:product:list")
    @GetMapping("/list")
    public TableDataInfo<XyywProductVo> list(XyywProductQuery query) {
        return xyywProductService.queryPageList(query);
    }

    /**
     * 导出产品列表
     */
    @SaCheckPermission("xyyw:product:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XyywProductQuery query, HttpServletResponse response) {
        List<XyywProductVo> list = xyywProductService.queryList(query);
        ExcelUtil.exportExcel(list, "产品", XyywProductVo.class, response);
    }

    /**
     * 获取产品详细信息
     *
     * @param productId 主键
     */
    @SaCheckPermission(value = {"xyyw:product:query", "xyyw:product:edit"}, mode = SaMode.OR)
    @GetMapping("/{productId}")
    public R<XyywProductVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer productId) {
        return R.ok(xyywProductService.queryById(productId));
    }

    /**
     * 新增产品
     */
    @SaCheckPermission("xyyw:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XyywProductBo bo) {
        return toAjax(xyywProductService.insertByBo(bo));
    }

    /**
     * 修改产品
     */
    @SaCheckPermission("xyyw:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XyywProductBo bo) {
        return toAjax(xyywProductService.updateByBo(bo));
    }

    /**
     * 删除产品
     *
     * @param productIds 主键串
     */
    @SaCheckPermission("xyyw:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] productIds) {
        return toAjax(xyywProductService.deleteWithValidByIds(List.of(productIds)));
    }
}
